package com.hyd.manage.service.impl;

import java.util.List;
import com.hyd.common.utils.DateUtils;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.mapper.MedicinesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hyd.manage.mapper.MedicineStockMapper;
import com.hyd.manage.domain.MedicineStock;
import com.hyd.manage.service.IMedicineStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 药品入库Service业务层处理
 * 
 * @author hwb
 * @date 2025-02-22
 */
@Service
public class MedicineStockServiceImpl implements IMedicineStockService 
{
    @Autowired
    private MedicineStockMapper medicineStockMapper;
    @Autowired
    private MedicinesMapper medicinesMapper;

    /**
     * 查询药品入库
     * 
     * @param id 药品入库主键
     * @return 药品入库
     */
    @Override
    public MedicineStock selectMedicineStockById(Long id)
    {
        return medicineStockMapper.selectMedicineStockById(id);
    }

    /**
     * 查询药品入库列表
     * 
     * @param medicineStock 药品入库
     * @return 药品入库
     */
    @Override
    public List<MedicineStock> selectMedicineStockList(MedicineStock medicineStock)
    {
        return medicineStockMapper.selectMedicineStockList(medicineStock);
    }

    /**
     * 新增药品入库
     * 
     * @param medicineStock 药品入库
     * @return 结果
     */
    @Override
    public int insertMedicineStock(MedicineStock medicineStock)
    {
        medicineStock.setCreateTime(DateUtils.getNowDate());
        return medicineStockMapper.insertMedicineStock(medicineStock);
    }

    /**
     * 修改药品入库
     * 
     * @param medicineStock 药品入库
     * @return 结果
     */
    @Override
    public int updateMedicineStock(MedicineStock medicineStock)
    {
        medicineStock.setUpdateTime(DateUtils.getNowDate());
        return medicineStockMapper.updateMedicineStock(medicineStock);
    }

    /**
     * 批量删除药品入库
     * 
     * @param ids 需要删除的药品入库主键
     * @return 结果
     */
    @Override
    public int deleteMedicineStockByIds(Long[] ids)
    {
        return medicineStockMapper.deleteMedicineStockByIds(ids);
    }

    /**
     * 删除药品入库信息
     * 
     * @param id 药品入库主键
     * @return 结果
     */
    @Override
    public int deleteMedicineStockById(Long id)
    {
        return medicineStockMapper.deleteMedicineStockById(id);
    }

    /**
     * TODO 有时候会异常入库，代码还需完善
     * 入库药品
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int storeMedicineStockByIds(Long[] ids) {
        // 查看库存中是否有药品批次号重复的
        for (Long id : ids) {
            MedicineStock medicineStock = medicineStockMapper.selectMedicineStockById(id);
            Medicines medicines = medicinesMapper.selectMedicineByCodeAndNumber(medicineStock.getMedicineCode(), medicineStock.getBatchNumber());
            if (medicines != null) {
                //为重复批次增加对应库存
                medicines.setStockQuantity(medicines.getStockQuantity() + medicineStock.getQuantity());
                medicines.setUpdateTime(DateUtils.getNowDate());
                //如果入库成功，则将入库状态改为已入库
                if (medicinesMapper.updateMedicines(medicines) > 0) {
                    medicineStock.setStockStatus("1");
                    medicineStockMapper.updateMedicineStock(medicineStock);
                }
            } else {
                if (medicineStockMapper.storeMedicineStockById(id) > 0) {
                    medicineStock.setStockStatus("1");
                    medicineStockMapper.updateMedicineStock(medicineStock);
                }
            }
        }
        // 返回结果
        return 1;
    }
}
